<?php
class Megadrupal_Slider_Helper_Data extends Mage_Core_Helper_Abstract
{
    
    const MEGA_IMAGE_FOLDER = 'megadrupal';
    
    public static function get_default_settings($key = NULL) {
        static $default_settings = array(
          'full_width' => 0,
          'width' => 960,
          'height' => 420,
          'touch_swipe' => 1,
          'responsive' => 1,
          'loadingbar' => 'bar',
          'bar_position' => 'bottom',
          'show_next_prev_button' => 1,
          'auto_play' => 1,
          'show_bullet' => 1,
          'bullet_position' => 5,
          'show_thumbnail' => 1,
          'thumbnail_position' => 1,
          'border_style' => 0,
          'dropshadow_style' => 0,
          'animation' => 'fade',
          'delay' => 8000,
          'transtime' => 800,
          'thumb_width' => 100,
          'thumb_height' => 75,
          'create_bg_imgstyle' => 1,
          'dmf_google' => '',
          'enable_font_css' => 0,
          'color_saved' => 'ff9900,CC0000',
        );

        if ($key) {
          return $default_settings[$key];
        }

        return $default_settings;
    }
    
    public function getAnimationOption() {        
        return array('fade'=>'Fade',
                    'scrollLeft'=>'Scroll left',
                    'scrollRight'=>'Scroll right',
                    'scrollHorz'=>'Scroll horizontal',
                    'scrollVert'=>'Scroll vertical',
                    'scrollUp'=>'Scroll up',
                    'scrollDown'=>'Scroll down'
                    );
    }
    
    public function getBarPosOption() {        
        return array('top'=>'Top',
                    'bottom'=>'Bottom'
                    );
    }
    public function getBulletPositon() {        
        return array('1'=>'Bottom left',
                    '2'=>'Bottom center',
                    '3'=>'Bottom right',
                    '4'=>'Top left',
                    '5'=>'Top center',
                    '6'=>'Top right'
                    );
    }
    
    public function getMegaImageFolder () {
        return self::MEGA_IMAGE_FOLDER;
    }

    public function getFullMegaImageUrl($img) {
        return Mage::getBaseUrl('media'). self::MEGA_IMAGE_FOLDER. '/'.$img;
    }
    
    public function preprocess_admin_slide_render($slide) {
        $defaultSetting = $this->get_default_settings();
        
        $slideSetting = $slide['settings'];
        $slideSetting['height']         = ($slideSetting['height'] > 0)? $slideSetting['height'] : $defaultSetting['height'];
        $slideSetting['width']          = ($slideSetting['width'] > 0)? $slideSetting['width'] : $defaultSetting['width'];
        $slideSetting['full_width']     = ($slideSetting['full_width'] > 0)? $slideSetting['full_width'] : $defaultSetting['full_width'];
        $slideSetting['touch_swipe']    = ($slideSetting['touch_swipe'] > 0)? $slideSetting['touch_swipe'] : $defaultSetting['touch_swipe'];
        $slideSetting['responsive']     = ($slideSetting['responsive'] > 0)? $slideSetting['responsive'] : $defaultSetting['responsive'];
        $slideSetting['loadingbar']     = ($slideSetting['loadingbar'] > 0)? $slideSetting['loadingbar'] : $defaultSetting['loadingbar'];
        $slideSetting['bar_position']   = ($slideSetting['bar_position'] > 0)? $slideSetting['bar_position'] : $defaultSetting['bar_position'];
        $slideSetting['auto_play']      = ($slideSetting['auto_play'] > 0)? $slideSetting['auto_play'] : $defaultSetting['auto_play'];
        $slideSetting['show_bullet']    = ($slideSetting['show_bullet'] > 0)? $slideSetting['show_bullet'] : $defaultSetting['show_bullet'];
        $slideSetting['bullet_position']= ($slideSetting['bullet_position'] > 0)? $slideSetting['bullet_position'] : $defaultSetting['bullet_position'];
        $slideSetting['show_thumbnail'] = ($slideSetting['show_thumbnail'] > 0)? $slideSetting['show_thumbnail'] : $defaultSetting['show_thumbnail'];
        $slideSetting['border_style']   = ($slideSetting['border_style'] > 0)? $slideSetting['border_style'] : $defaultSetting['border_style'];
        $slideSetting['animation']      = ($slideSetting['animation'] > 0)? $slideSetting['animation'] : $defaultSetting['animation'];
        $slideSetting['delay']          = ($slideSetting['delay'] > 0)? $slideSetting['delay'] : $defaultSetting['delay'];
        $slideSetting['transtime']      = ($slideSetting['transtime'] > 0)? $slideSetting['transtime'] : $defaultSetting['transtime'];
        $slideSetting['thumb_width']    = ($slideSetting['thumb_width'] > 0)? $slideSetting['thumb_width'] : $defaultSetting['thumb_width'];
        $slideSetting['thumb_height']   = ($slideSetting['thumb_height'] > 0)? $slideSetting['thumb_height'] : $defaultSetting['thumb_height'];
        $slideSetting['dmf_google']     = ($slideSetting['dmf_google'] > 0)? $slideSetting['dmf_google'] : $defaultSetting['dmf_google'];
        $slideSetting['enable_font_css']= ($slideSetting['enable_font_css'] > 0)? $slideSetting['enable_font_css'] : $defaultSetting['enable_font_css'];
        $slideSetting['color_saved']    = ($slideSetting['color_saved'] > 0)? $slideSetting['color_saved'] : $defaultSetting['color_saved'];
        $slideSetting['create_bg_imgstyle']     = ($slideSetting['create_bg_imgstyle'] > 0)? $slideSetting['create_bg_imgstyle'] : $defaultSetting['create_bg_imgstyle'];
        $slideSetting['dropshadow_style']       = ($slideSetting['dropshadow_style'] > 0)? $slideSetting['dropshadow_style'] : $defaultSetting['dropshadow_style'];
        $slideSetting['show_next_prev_button']  = ($slideSetting['show_next_prev_button'] > 0)? $slideSetting['show_next_prev_button'] : $defaultSetting['show_next_prev_button'];
        $slideSetting['thumbnail_position']     = ($slideSetting['thumbnail_position'] > 0)? $slideSetting['thumbnail_position'] : $defaultSetting['thumbnail_position'];
        
        $slide['settings'] = $slideSetting;
        
        return $slide;
    }
    
    public function preprocess_admin_layers_render(&$variables) {
        $layer = $variables['layer'];
        
        $data = array();
        foreach ($layer as $attr => $value) {
            $data[] = "data-{$attr}='{$value}'";
        }
        $variables['data'] = implode(' ', $data);

        if ($layer['type'] == 'image') {
            $layer['file_url'] = '';
            if (isset($layer['fileid'])) {
                $layer['file_url'] = $this->getFullMegaImageUrl($layer['fileid']);
            }
        }

        $variables['layer'] = $layer;

        $style = array();
        $style[] = "z-index: {$layer['zindex']};";
        $style[] = "width: {$layer['width']}px;";
        $style[] = "height: {$layer['height']}px;";
        $style[] = "top: {$layer['top']}px;";
        $style[] = "left: {$layer['left']}px;";
        $variables['styles'] = implode(' ', $style);
        
        return $variables;
    }    
    
    public function processSlideSettings($settings) {
        $settings['background_image'] = $this->getFullMegaImageUrl($settings['background_image']);        
        $settings['data_properties'] = "data-thumb='".$this->getFullMegaImageUrl($settings['custom_thumbnail'])."' data-timeout='".$settings['timelinewidth']."00' style='height: ".$settings['height'].";'";        
        $settings['background_style'] = "style='width: ".$settings["width"]."'; height: ".$settings["height"]."px'; height: ".$settings["height"]."px;";
        
        return $settings;
    }
    
    public function processLayerData($element_full) {
        $element_full['starttime'] = $element_full['starttime'];
        $element_full['stoptime'] = $element_full['stoptime'];

        $output = array();
        $output[] = "data-x='{$element_full['left']}'";
        $output[] = "data-y='{$element_full['top']}'";
        $output[] = "data-width='{$element_full['width']}'";
        $output[] = "data-height='{$element_full['height']}'";
        $output[] = "data-start='{$element_full['starttime']}'";
        $output[] = "data-stop='{$element_full['stoptime']}'";
        if ($element_full['startani'] != 'none' && $element_full['startani'] != '') {
          $output[] = "data-easein='{$element_full['startani']}'";
        }
        if ($element_full['stopani'] != 'none' && $element_full['stopani'] != '') {
          $output[] = "data-easeout='{$element_full['stopani']}'";
        }

        if (isset($element['paddingtop']) && $element['paddingtop'] > 0) {
          $output[] = "data-padding-top='{$element_full['paddingtop']}'";
        }

        if (isset($element['paddingbottom']) && $element['paddingbottom'] > 0) {
          $output[] = "data-padding-bottom='{$element['paddingbottom']}'";
        }

        if (isset($element['paddingleft']) && $element['paddingleft'] > 0) {
          $output[] = "data-padding-left='{$element['paddingleft']}'";
        }

        if (isset($element['paddingright']) && $element['paddingright'] > 0) {
          $output[] = "data-padding-right='{$element['paddingright']}'";
        }
        
        $element_full['data'] = implode(' ', $output);
        
        
        
        # Process styles for text layer
        if ($element_full['type'] == 'text') {
          $outputStyles = array();
          if (isset($element_full['color'])) {
            $outputStyles[] = "color: #{$element_full['color']};";
          }

          if (isset($element_full['bordercolor'])) {
            $outputStyles[] = "border-color: #{$element_full['bordercolor']};";
          }

          if (isset($element_full['textalign'])) {
            $outputStyles[] = "text-align: {$element_full['textalign']};";
          }

          if (isset($element_full['fontsize'])) {
            $fontsize = $element_full['fontsize'] / 12;
            $outputStyles[] = "font-size: {$fontsize}em;";
          }

          if (isset($element_full['fontweight'])) {
            $outputStyles[] = "font-weight: {$element_full['fontweight']};";
          }

          if (isset($element_full['fontfamily'])) {
            $outputStyles[] = "font-family: {$element_full['fontfamily']};";
          }

          if (isset($element_full['fontstyle'])) {
            $outputStyles[] = "font-style: {$element_full['fontstyle']};";
          }

          if (isset($element_full['textdecoration'])) {
            $outputStyles[] = "text-decoration: {$element_full['textdecoration']};";
          }

          if (isset($element_full['texttransform'])) {
            $outputStyles[] = "text-transform: {$element_full['texttransform']};";
          }
        }
        $element_full['styles'] =  implode(' ', $outputStyles);

        return $element_full;
    }
}
	 